using System;
using System.Web;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Users;
using DotNetNuke.Modules.Announcements;
using ITCrossing;
using DataProvider = DotNetNuke.Data.DataProvider;

namespace MetaPostProvider.Announcements
{
    public class AnnouncementsInfoAdapter
    {
        public static void FillAnnouncementInfo(ref AnnouncementInfo announcementInfo, Item item, int moduleId, UserInfo userInfo)
        {
            announcementInfo.ModuleId = moduleId;
            announcementInfo.ItemId = Convert.ToInt32(item.ItemId);
            // TODO: See footnote 1 in this file...
            announcementInfo.Title = HttpUtility.HtmlDecode(item.Title); // item.Title; //
            announcementInfo.Description = HttpUtility.HtmlEncode(item.Content); // item.Content; // 

            // Default behaviour is to always set the Created Date+User to the current info, whether updating or adding. This information is shown in the Edit view using the DNNAudit control.
            announcementInfo.CreatedDate = DateTime.Now;
            announcementInfo.CreatedByUser = userInfo.UserID;

            DateTime PubDate;
            if (item.StartDate == DateTime.MinValue)
            {
                PubDate = DateTime.Now;
            }
            else
            {
                PubDate = item.StartDate.ToLocalTime().Date;
            }
            announcementInfo.PublishDate = PubDate;
        }

        public static Item ExtractAnnouncemntInfo(AnnouncementInfo info, int portalId)
        {
            Item itemInfo = new Item();

            // Note: there is no equivalent in the Item struct for the AnnouncementInfo.ModuleId
            itemInfo.ItemId = info.ItemId;
            itemInfo.Title = info.Title; // HttpUtility.HtmlDecode(info.Title); //
            itemInfo.Content = HttpUtility.HtmlDecode(info.Description); // info.Description; // 

            itemInfo.DateCreated = info.CreatedDate;
            // Note: Get the actual AnnouncementInfo's created user, even if
            //       it isn't the same as the user accessing this announcement
            //       (multiple users may edit/use this data)
            UserController controller = new UserController();
            // TODO: Consider replacing portalId variable with the UserController.PortalId property, if it is accurately generated...
            UserInfo user = controller.GetUser(portalId, info.CreatedByUser);
            if (user == null)
                itemInfo.AuthorId = String.Empty;
            else
                itemInfo.AuthorId = user.DisplayName;

            itemInfo.StartDate = info.PublishDate;

            return itemInfo;
        }
    }
}
/* Footnote 1: HTML in the Title....
 *              It appears that the browser editor allows HTML tags to be put into the title, then rendered as actual HTML in view mode. To "mimick" that in WLW/metaPost, I need to HTMLDecode the title when filling the AnnouncementsInfo (for setting), but do nothing when getting the into (for extracting).
 */